﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelnetRouterApplication.Control;
using TelnetRouterApplication.Model;
using TelnetRouterApplication.Utils;

namespace TelnetRouterApplication.View
{
    public partial class CheckConnection : UserControl
    {
        private MainForm _mainForm;
        public MainForm MainForm
        {
            get { return _mainForm; }
            set { _mainForm = value; }
        }

        private List<Interface> _interfaceList;
        public List<Interface> InterfaceList
        {
            get { return _interfaceList; }
            set { _interfaceList = value; }
        }

        public CheckConnection(MainForm mainForm)
        {
            MainForm = mainForm;

            InitializeComponent();
            verifyInterfaces();
        }

        public CheckConnection() : this(null) { }

        private void runBtn_Click(object sender, EventArgs e)
        {
            if (verifyInterfaces())
            {
                //Pelo menos uma está ligada

                //Contar quantas estão on para a barra de load
                int numberOn = 0;
                foreach (Interface inter in InterfaceList)
                {
                    if (inter.Shutdown && inter.Protocol)
                    {
                        numberOn++;
                    }
                }

                bool success = true;

                foreach (Interface inter in InterfaceList)
                {
                    //Lembrar de bloquear os botões
                    if (inter.Shutdown && inter.Protocol)
                    {
                        bool pingSuccess = CommandHandler.GetInstance().PingInterface(inter);
                        progressBar.Increment(100/numberOn);

                        LogPing(inter, pingSuccess);

                        //Acumulando os sucessos
                        success &= pingSuccess;
                    }
                }

                if (success)
                {
                    testLabel.Text = "Success!";
                    resultPanel.BackColor = Color.Green;
                }
                else
                {
                    testLabel.Text = "Failed!";
                    resultPanel.BackColor = Color.Red;
                }
            }
        }

        private void cleanBtn_Click(object sender, EventArgs e)
        {
            logBox.Clear();
        }

        private void LogPing(Interface inter, bool success)
        {
            string log = "";
            if (success)
            {
                log += "Successfull";
            }
            else
            {
                log += "Failed";
            }

            log += " connection on interface : " + inter.Name + "\r\n";

            logBox.Text = log + logBox.Text;
        }

        private void AddToLog(string text)
        {
            logBox.Text = text + logBox.Text;
        }

        private bool verifyInterfaces()
        {
            InterfaceList = CommandHandler.GetInstance().GetInterfaces();

            //Para verificar se pelo menos uma está ligada
            bool atLeastOneTurnedOn = false;
            foreach (Interface inter in InterfaceList)
            {
                if (inter.Shutdown && inter.Protocol)
                {
                    atLeastOneTurnedOn = true;
                }
            }

            if (!atLeastOneTurnedOn)
            {
                string message = "All interfaces are off!";
                MessageBox.Show(message);
                AddToLog(message);
            }

            return atLeastOneTurnedOn;
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {
            string path = IOUtils.SaveFileDialogShow(MainForm, "Text Files|*.txt");
            IOUtils.CreateTxtFile(path, logBox.Text);
            MessageBox.Show("Log exported!");
        }
    }
}

